<?php
defined('_JEXEC') or die();

class WrdControllerComplain extends JControllerLegacy 
{
        protected $option;
        protected $controller; 
		function __construct( $default = array()){
    		$this->option = 'com_wrd';
    		$this->controller = 'complain';
			
			parent::__construct( $default );
    	}

      	function display()
 		{
			switch($this->getTask()){
				case 'add':{
						JRequest::setVar( 'hidemainmenu', 1 );
						JRequest::setVar( 'layout', 'form'  );
						JRequest::setVar( 'view'  , 'complain');
					} 
					break;
	            default:
					JRequest::setVar( 'view'  , 'complain');
	            	break;
			}
			parent::display();
        }
        
      //save profile info
    function save(){
      
	   $app = JFactory::getApplication() ;  
	   $db = JFactory::getDbo();
       $post=JRequest::get('post');
       $post['created_date'] = date("Y-m-d H:i:s");
		//echo '<pre>';print_r($post);die;
	   $params = JComponentHelper::getParams('com_wrd');
       $model = $this->getModel('complain');
       $sql = 'SELECT name,email,action,subject,template,template_vi FROM #__wrd_cemail WHERE action="complaints"';
	   $db->setQuery($sql);
	   $row = $db->loadObject();
	   
	   $from = 'info@wmcvip.vn';
	   $fromName = 'WMCVIP';
	   $subject  = $row->subject;
	   $subject = str_replace('{member_id}', $post['vip_number'], $subject);
	   $subject = str_replace('{outlet}', $post['outlet'], $subject);
	   $subject = str_replace('{visit_date}',date('d-m-Y',strtotime($post['visit_date'])), $subject);
	  
	  $temp = $row->template ;
	  $to = explode(',', $row->email) ;
	  
	   $temp = str_replace('{member_id}', $post['vip_number'], $temp);
	   $temp = str_replace('{name}', $post['guest_name'], $temp);
	   $temp = str_replace('{phone}', $post['phone'], $temp);
	   $temp = str_replace('{dob}', date('m-d-Y',strtotime($post['dob'])), $temp);
	   $temp = str_replace('{job}', $post['job'], $temp);
	   $temp = str_replace('{joining_date}', date('m-d-Y',strtotime($post['joining_date'])), $temp);
	   $temp = str_replace('{outlet}', $post['outlet'], $temp);
	   $temp = str_replace('{point}', $post['current_point'], $temp);
	   $temp = str_replace('{created_date}', date('d-m-Y H:i:s',strtotime($post['created_date'])), $temp);
	   
	   $temp = str_replace('{comment}', $post['complain'], $temp);
	   $temp = str_replace('{action_taken}', $post['action_taken'], $temp);
	   $temp = str_replace('{complaint_type}', $post['ctype'], $temp);
	   $temp = str_replace('{meal_period}', $post['mperiod'], $temp);
	   $temp = str_replace('{visit_date}', date('d-m-Y',strtotime($post['visit_date'])), $temp);
	   $temp = str_replace('{handle_by}', $post['h_by'], $temp);
       
	   
	   
	   foreach($to as $key => $email):
			//echo $email.'<br/>';
			$return = JUtility::sendMail($from, $fromName, $email, $subject, $temp,true);
			
			if ($return !== true) {
				$this->setError(JText::_('COM_USERS_REGISTRATION_SEND_MAIL_FAILED'));
			}
			
	   endforeach;
	   //die;
	   $id_category = $model->store($post,'complain');
	   
       if($id_category!=0){
			/*
			if(!empty($post['complain_flag'])){
				//exit();
				if($post['forward'] == 1){
					$this->setRedirect('index.php?option=com_wrd&controller=profile&id='.$post['complain_flag'].'&layout=form');
				}else{
					$this->setRedirect('index.php?option=com_wrd&controller=complain','Saved');
				}
				$app->setUserState('complain_flag', '') ;
				$app->setUserState('user_id', '') ;
			}else{
       			$this->setRedirect('index.php?option=com_wrd&controller=complain','Saved');
			}
			*/
			$this->setRedirect('index.php?option=com_wrd&controller=profile&id='.$post['complain_flag'].'&layout=form&tab=complains','Saved');
			return true;                
       }
       else{
            $this->setRedirect('index.php?option=com_wrd&controller=complain','Error Saved');
            return false;
       }
    }    
	function cancel(){
		$post=JRequest::get('post');
		
		$this->setRedirect('index.php?option=com_wrd&controller=profile&id='.$post['complain_flag'].'&layout=form&tab=complains');
	}
    function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('complain');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=complain' );
	}
   
    function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>' ) );
		}

		$model = $this->getModel('complain');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=complain' );
	}

	function unpublish(){
		
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>'  ) );
		}

		$model = $this->getModel('complain');
		
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		
		$this->setRedirect( 'index.php?option=com_wrd&controller=complain' );
	}
}